<template>
	<div ref="container">
		<div style="position: absolute; z-index: 10" class="flex-center w100 h100" v-show="loading">
			<dv-decoration :scale="2">
				<span class="fw" style="font-size: 30px"> {{ percentComplete }}% </span>
			</dv-decoration>
		</div>
		<my-compass :keyType="keyType" />
	</div>
</template>

<script setup name="drain">
import myLabel from '@c/common/my-label.vue'
import { firstPumpInfo } from '@a/drain'
import { DBUtil } from '@u/indexedDB'
import { useResizeObserver, THREE, GLTFloaderFn, cameraFn, rendererFn, labelRendererFn, CSS3DSprite, compassRotate, keyType } from '@u/threeMinx'

let container = shallowRef(null), // 模型渲染的Demo节点
	renderer, //  WebGLRenderer渲染器
	scene, // 场景
	camera, // 相机
	cameraRender,
	labelRenderer,
	loading = ref(false),
	percentComplete = ref(0)

// 组件宽高
let width, height
useResizeObserver(container, (entries) => {
	width = entries[0].contentRect.width
	height = entries[0].contentRect.height
	camera.aspect = width / height
	camera.updateProjectionMatrix()
	renderer.setSize(width, height)
	labelRenderer.setSize(width, height)
})
// 初始化生命周期
onMounted(() => {
	loading.value = true
	init()
})
onBeforeUnmount(() => {
	cancelAnimationFrame(requestAnimationFrameIndex)
	// 释放显存
	renderer?.dispose()
	// 释放内存
	container = null
	scene = null
	labelRenderer = null
})

function init() {
	// 场景
	scene = new THREE.Scene()
	// 相机
	camera = new THREE.PerspectiveCamera(45, width / height, 1, 2000)
	camera.position.set(0, 4, 0)
	// camera.lookAt(80, -8, -3);
	cameraRender = cameraFn(camera)
	scene.add(camera)
	// 渲染器 // antialias开启抗锯齿 alpha开启背景透明
	renderer = rendererFn(container)
	renderer.shadowMap.enabled = true
	labelRenderer = labelRendererFn(container)
	// 灯光
	lights()
	// 加载器
	loaderFn()
}
let objects = [],
	model,
	labelObj = new THREE.Group()

function loaderFn() {
	new DBUtil().get(`${import.meta.env.VITE_APP_BASE_BASE || '/'}model/供排水系统.glb`).then((blob) => {
		let url = URL.createObjectURL(blob)
		GLTFloaderFn.load(
			url,
			(object) => {
				scene.add(labelObj)
				labelFn()
				loading.value = false
				model = object.scene
				objects = model.getObjectByName('墙体').children
				scene.add(model)
				// 顶灯
				for (let i = 1; i < 10; i++) {
					const mc = model.getObjectByName('顶灯').clone()
					mc.position.z = mc.position.z - i * 12
					scene.add(mc)
				}
				// 水泵
				for (let i = 1; i < 9; i++) {
					const mc = model.getObjectByName('水泵').clone()
					mc.position.z = mc.position.z - i * 12.6
					scene.add(mc)
				}
				// 水泵箱
				for (let i = 1; i < 15; i++) {
					const mc = model.getObjectByName('水泵箱').clone()
					mc.position.z = mc.position.z - i * 8
					scene.add(mc)
				}
				// 电箱
				for (let i = 1; i < 6; i++) {
					const mc = model.getObjectByName('电箱').clone()
					mc.position.z = mc.position.z - i * 18.5
					scene.add(mc)
				}
				render()
			},
			(xhr) => {
				if (xhr.lengthComputable) percentComplete.value = Math.round((xhr.loaded / xhr.total) * 100, 2)
			}
		)
	})
}

/**
 * laebl标签
 */
function labelFn() {
	firstPumpInfo().then((res) => {
		labelObj.clear()
		const { x, y, z } = model.getObjectByName('顶灯').clone().position
		res.data.forEach((item, i) => {
			const dom = createApp(myLabel, {
				data: {
					name: item.valveName,
					list: [
						{
							title: '阀位',
							val: item.valveValue,
							suffix: '%'
						}
					]
				},
				callBack: function () {}
			}).mount(document.createElement('div')).$el
			const pointLabel = new CSS3DSprite(dom)
			pointLabel.position.set(x, y - 3, z - (i + 1) * 12)
			pointLabel.scale.set(0.02, 0.02, 0.02)
			labelObj.add(pointLabel)
		})
	})
}

/**
 * 灯光
 */
function lights() {
	// 自然光
	scene.add(new THREE.AmbientLight('#ffffff', 0.5))
	let light2 = new THREE.PointLight('#ffffff', 0.5)
	light2.position.set(0, 15, -30)
	scene.add(light2)
}

// 渲染逻辑
let requestAnimationFrameIndex
function render() {
	keyType.value = cameraRender(objects)
	compassRotate(camera)
	labelRenderer?.render(scene, camera)
	renderer?.render(scene, camera)
	requestAnimationFrameIndex = requestAnimationFrame(render)
}
</script>
